
package artistic;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * Class that handles color selection
 * and keeps track of foreground and
 * background colors
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */
public class ColorChooser extends JDialog{
    /**
     * Internal class for the color preview
     */
    private class ColorPreview extends JPanel implements ChangeListener
    {
        /**
         * Internal class for individual color views
         */
        private class ColorView extends JPanel implements MouseListener
        {
            private Color color;
            private boolean isActive = false;

            ColorPreview owner;

            public ColorView(ColorPreview owner)
            {
                setPreferredSize(new Dimension(20, 20));
                this.owner = owner;
                this.addMouseListener(this);
            }
            
            private void drawColorBox
                (Graphics g, int x, int y, Color c, boolean selected)
            {
                g.setColor(c);
                g.fillRect(x, y, 20, 20);
                if(selected)
                    g.setColor(Color.BLACK);
                else
                    g.setColor(Color.WHITE);
                g.drawRect(x, y, 20, 20);
            }
            
            public void setColor(Color c)
            {
                color = c;
            }

            public void setActive(boolean state)
            {
                isActive = state;
            }

            @Override
            protected void paintComponent(Graphics g)
            {
                drawColorBox(g, 0, 0, color, isActive);
            }

            public void mouseClicked(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {
                owner.viewSelected(this);
                isActive = true;
            }
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
        }

        private ColorChooser owner;
        private ColorView bgView, fgView;

        /**
         * Called by ColorView when selected
         * 
         * @param view
         */
        public void viewSelected(ColorView view)
        {
            if(view==fgView)
            {
                owner.setFgActive(true);
                bgView.setActive(false);
            }
            if(view==bgView)
            {
                owner.setFgActive(false);
                fgView.setActive(false);
            }
            repaint();
        }

        public void updateColors()
        {
            bgView.setColor(new Color(owner.getBgColor()));
            fgView.setColor(new Color(owner.getFgColor()));
        }

        public ColorPreview(ColorChooser owner)
        {
            setPreferredSize(new Dimension(100,50));
            setSize(new Dimension(100,50));
            this.owner = owner;

            bgView = new ColorView(this);

            fgView = new ColorView(this);
            fgView.setActive(true);

            updateColors();

            this.add(new JLabel("Foreground"));
            this.add(fgView);

            this.add(new JLabel("Background"));
            this.add(bgView);

            setVisible(true);
        }

        public void stateChanged(ChangeEvent e) {
            owner.updateColor();
            updateColors();
            repaint();
        }
    }

    JColorChooser widget;

    ColorPreview preview;
    
    public ColorChooser()
    {
        setLayout(new FlowLayout(FlowLayout.CENTER, 0, 2));

        widget = new JColorChooser(new Color(fg));

        widget.setPreviewPanel(new JPanel());
        
        setSize(new Dimension(500,400));

        this.add(widget);
        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        preview = new ColorPreview(this);
        widget.getSelectionModel().addChangeListener(preview);

        this.add(preview);

        setVisible(true);
    }

    private int fg = 0xff000000;
    private int bg = 0xffffffff;

    private boolean isFgActive = true;

    public void updateColor()
    {
        setColor(widget.getColor().getRGB() | 0xff000000);
    }

    public int getFgColor()
    {
        return fg;
    }

    public int getBgColor()
    {
        return bg;
    }

    public int getColor()
    {
        if(isFgActive)
            return getFgColor();
        else
            return getBgColor();
    }

    public void setFgColor(int color)
    {
        fg = color;
    }

    public void setBgColor(int color)
    {
        bg = color;
    }

    public void setColor(int color)
    {
        if(isFgActive)
            setFgColor(color);
        else
            setBgColor(color);
        widget.setColor(color);
    }

    public void setFgActive(boolean state)
    {
        isFgActive = state;
    }

    public boolean getFgActive()
    {
        return isFgActive;
    }

    @Override
    public void update(Graphics g)
    {
        preview.repaint();
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        preview.repaint();
    }
}
